package com.example.psysystemhomework.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.example.psysystemhomework.bean.StoryBean;
import com.example.psysystemhomework.bean.UserBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class ShareUtil {
    public static String user_key = "user";
    public static String db = "psy";
    public static String story_key = "story";
    public static String host_key = "hostKey";
    public Context context;
    public static UserBean userBean = new UserBean();

    public  static  ShareUtil shareUtil;
    public ShareUtil(Context context) {
        this.context = context;
    }

    public void saveUser(String data) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(db, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(user_key, data);
        edit.commit();
    }

    public UserBean getUser() {
        UserBean userBean = null;
        SharedPreferences sharedPreferences = context.getSharedPreferences(db, Context.MODE_PRIVATE);
        String string = sharedPreferences.getString(user_key, null);

        if (string == null) {
            Toast.makeText(context, "请先登录", Toast.LENGTH_SHORT).show();
        } else {
            Gson gson = new Gson();
            try {
                JSONObject jsonObject = new JSONObject(string);
                userBean = gson.fromJson(jsonObject.get("data").toString(), UserBean.class);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        return userBean;
    }

    public void saveContent(String title, String content, boolean isClear) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(db, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        if (isClear) {
            edit.putString(story_key, null);
        } else {
            StoryBean storyBean = new StoryBean(content, title);
            edit.putString(story_key, storyBean.toString());
        }
        edit.commit();
    }

    public StoryBean getStoryDraft() {
        SharedPreferences sharedPreferences = context.getSharedPreferences(db, Context.MODE_PRIVATE);
        String string = sharedPreferences.getString(story_key, null);
        if (string != null) {
            Gson gson = new Gson();
            StoryBean storyBean = gson.fromJson(string, StoryBean.class);
            return storyBean;
        } else {
            return null;
        }
    }



    public void saveHost(String host) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(db, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(host_key,host);
        edit.commit();
    }

    public  String getHost()
    {
        SharedPreferences sharedPreferences = context.getSharedPreferences(db, Context.MODE_PRIVATE);
        return  sharedPreferences.getString(host_key,"192.168.6.64:8081");
    }


}
